package t1004;


import java.util.Scanner;

/**
 * 姓名： 张东升
 * 日期: 2025年03月2025/3/17日
 */
public class Main {

    private static int findNumber;
    private static int nodeLength;

    public static void main(String[] args) {
        // 初始化单链表
        Scanner sc = new Scanner(System.in);
        Node head = new Node();
        initLink(sc, head);
        sort(head);
        display(head,true);
        sort(head);
        display(head,false);


    }

    /**
     * 初始化数据
     *
     * @param sc   键盘输入
     * @param head 头节点
     */
    public static void initLink(Scanner sc, Node head) {
        if (head == null) {
            head = new Node();
        }

        int size = 0;
        Node temp = head;


        while (sc.hasNext()) {
            int data = sc.nextInt();
            if (data != 0) {
                temp.setNext(new Node());
                temp = temp.getNext();
                temp.setData(data);
                size++;
            } else {
                break;
            }
        }
        // 最后要查找的数字
        findNumber = sc.nextInt();
        nodeLength = size;
    }


    /**
     * 输出链表数据
     *
     * @param head 头部节点
     */
    public static void display(Node head, boolean showFound) {
        Node temp = head.getNext();
        int index = 0;
        int n = 0;
        System.out.println("The length:" + nodeLength);
        System.out.println("The elements:");
        while (temp != null) {
            System.out.printf("%d ", temp.getData());

            if (findNumber == temp.getData()) {
                index = n;
            }
            temp = temp.getNext();
            n++;
        }
        if (showFound) {
            System.out.println();
            System.out.println(index != 0 ? ("Found position:" + (index + 1)) : ("no Found"));
        }
    }


    /**
     * 逆序单链表
     *
     * @param head 头节点
     */
    public static void sort(Node head) {
        // 指向头节点
        Node next = head.getNext();
        Node h = head.getNext();
        Node pre = head.getNext();
        Node link = head;
        int n = nodeLength;
        for (int i = 0; i < n; i++) {
            while (next != null) {
                if (next.getNext() == null) {
                    pre.setNext(null);
                    // 开始逆序
                    link.setNext(next);
                    link = link.getNext();
                    next = h;
                    break;
                }
                pre = next;
                next = next.getNext();
            }
            head = link;
        }
    }

}


class Node {
    private Node next;
    private int data;

    public Node(Node next, int data) {
        this.data = data;
        this.next = next;
    }

    public Node() {
    }

    public int getData() {
        return data;
    }

    public void setData(int data) {
        this.data = data;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }
}
